package com.sd.repay.auth;

import com.sd.repay.pojo.RealAuthChannel;
import com.sd.repay.service.RealAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class RealAuthChannelCache {

    private static Map<String, Object> realChannelCache = new HashMap<>();

    private static RealAuthService realAuthService;

    @Autowired
    private RealAuthService realAuthService2;

    @Autowired
    RealAuthChannelCache(RealAuthService realAuthService) {
        RealAuthChannelCache.realAuthService = realAuthService;
    }

    @PostConstruct
    public void beforeInit() {
        realAuthService = realAuthService2;
    }

    public static void getTransferChannel() {
        List<RealAuthChannel> channelList = realAuthService.getRealAuthChannel();
        for (RealAuthChannel realAuthChannel : channelList) {
            realChannelCache.put(realAuthChannel.getChannelCode(), realAuthChannel);
        }
    }

    public static Object getChannel(String channelKey) {
        Object channelObj = realChannelCache.get(channelKey);
        if (channelObj == null) {
            getTransferChannel();
            channelObj = realChannelCache.get(channelKey);
        }

        return channelObj;

    }
}
